package com.hfh.jdbc;

import com.alibaba.druid.pool.DruidDataSource;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @Author hfh
 * @Date 2022 09 20 20 05
 **/
public class DSUtils {
    private static DruidDataSource dataSource = new DruidDataSource();
    public static ThreadLocal<Connection> threadLocal = new ThreadLocal<>();

    static {
        try {
            Properties pros = new Properties();
            InputStream is = DruidDemo.class.getClassLoader()
                    .getResourceAsStream("jdbc.properties");
            pros.load(is);

            dataSource.setUrl(pros.getProperty("url"));
            dataSource.setUsername(pros.getProperty("user"));
            dataSource.setPassword(pros.getProperty("password"));
            dataSource.setInitialSize(Integer.valueOf(pros.getProperty("initialSize")));
            dataSource.setMaxActive(Integer.valueOf(pros.getProperty("maxActive")));
            dataSource.setMinIdle(Integer.valueOf(pros.getProperty("minldle")));
            dataSource.setMaxWait(Integer.valueOf(pros.getProperty("maxWait")));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

    public static void close(Connection connection, PreparedStatement statement) {
        try {
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /*
  通用的修改方法, 兼容事务
   */
    public static void update(String sql, Object... args) {
        // 获取连接
        Connection connection = threadLocal.get();

        boolean flag = connection == null;

        if (flag) { // 不需要事务, 在方法里面创建连接
            try {
                connection = dataSource.getConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        // 编译有占位符的sql
        try (PreparedStatement ps = connection.prepareStatement(sql)) {


            // 设置参数
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            // 执行sql语句
            int num = ps.executeUpdate();

            System.out.println("影响了" + num + "行记录");

        } catch (SQLException e) {
            e.printStackTrace();


        } finally {
            if (flag) {
                // 关闭连接
                if (connection != null) {
                    try {
                        connection.close();
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        }
    }

    public static void changeRecordSafe(String sql, Object... args) {
        // 获取连接
        Connection connection = threadLocal.get();
        try (
                // 编译有占位符的sql
                PreparedStatement ps = connection.prepareStatement(sql)) {
            // 获取数据库操作对象（专门执行sql语句的对象）

            // 设置参数
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            // 执行sql语句
            int num = ps.executeUpdate();

            System.out.println("影响了" + num + "行记录");

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}
